package com.zcb.agrimarket.service.impl;

import com.zcb.agrimarket.constant.MessageConstant;
import com.zcb.agrimarket.context.BaseContext;
import com.zcb.agrimarket.exception.AccountNotFoundException;
import com.zcb.agrimarket.pojo.User;
import com.zcb.agrimarket.mapper.UserMapper;
import com.zcb.agrimarket.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcb.agrimarket.utils.MailUtilsQQ;
import com.zcb.agrimarket.utils.MyCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zcb
 * @since 2024-04-03
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;

    public User getByaccount(String account,String password) {
        User user = userMapper.selectByaccount(account,password);
        if(user == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        return user;
    }

    public User getBymailbox(String account,String mailbox) {
        User user = userMapper.selectBymailbox(account,mailbox);
        if(user == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        return user;
    }

    public void getCode(String mailboc) {
        MailUtilsQQ.sendMail(mailboc);
    }

    public Boolean updatePassword(String account,String password,String mailbox, String verifyCode){

        log.info(verifyCode+" "+(String) MyCache.get(mailbox)+" "+!verifyCode.equals(MyCache.get(mailbox)));
        if(!verifyCode.equals((String) MyCache.get(mailbox))) return false;
        userMapper.updatePassword(account,password);
        return true;
    }

    public Boolean deleteByMyid(Long id){
        userMapper.deleteByMyid(id);
        return true;
    }
}
